<?php
header('Content-Type: application/json; charset=utf-8');

// 引入数据库配置
$dbConfig = include('db_config.php');

// 获取前端传过来的邮箱信息
$email = $_POST['邮箱'] ?? '';

// 检查邮箱是否为空
if (empty($email)) {
    $result = [
        'status' => 'error',
        'message' => '邮箱不能为空'
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
    exit;
}

// 创建数据库连接
try {
    $dsn = "mysql:host=" . $dbConfig['host'] . ";dbname=" . $dbConfig['dbname'] . ";charset=" . $dbConfig['charset'];
    $pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 构建查询语句
    $selectStmt = "SELECT * FROM mi_users WHERE 邮箱 = :email";

    // 准备并执行查询语句
    $stmt = $pdo->prepare($selectStmt);
    $stmt->execute([':email' => $email]);
    $user = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($user) {
        // 返回用户所有信息
        $result = [
            'status' => 'success',
            'message' => '获取用户信息成功',
            'user' => $user
        ];
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
    } else {
        // 用户不存在
        $result = [
            'status' => 'error',
            'message' => '用户不存在'
        ];
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
    }

} catch (PDOException $e) {
    // 数据库操作失败
    $result = [
        'status' => 'error',
        'message' => '数据库操作失败：' . $e->getMessage()
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
}
?>
